Skip to content

feat: add balance metadata for snap datasource [DO NOT MERGE]#9564

Open
stanleyyconsensys wants to merge 2 commits into
mainfrom
feat/asset-ctrl-balance-metadata
Open

feat: add balance metadata for snap datasource [DO NOT MERGE]#9564
stanleyyconsensys wants to merge 2 commits into
mainfrom
feat/asset-ctrl-balance-metadata

Conversation

@stanleyyconsensys

@stanleyyconsensys stanleyyconsensys commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Explanation

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Low Risk
Additive, optional field on balance objects in a single data source; no auth or breaking contract changes in this diff.

Overview
Snap balances can now carry optional per-asset metadata through the assets pipeline when they come from keyring snaps.

SnapDataSource extends the AccountsController:accountBalancesUpdated payload typing with optional metadata on each balance entry, and copies that field into assetsBalance when handling live snap updates and when mapping getAccountBalances results during fetch. Metadata is only attached when the snap provides it; amounts are unchanged.

Reviewed by Cursor Bugbot for commit a3e3158. Bugbot is set up for automated code reviews on this repo. Configure here.

@stanleyyconsensys
stanleyyconsensys requested a review from a team as a code owner July 21, 2026 04:34
[assetId: string]: {
amount: string;
unit: string;
metadata?: Record<string, Json>;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add metadata into snap balance update event payload type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want type enforced payloads?

E.g. a typed descriminated union?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe we can enforce a schema for runtime validation & parsing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think in future
some snap may still using SNAP path to sync data, those data will not able to predict

adding validation may also adding a complexity on how snap ship data to controller

and it require bump up to controller when the data sharp change..., so i will suggest leave it as this (should align with keyring API)

accountAssets ??= {};
accountAssets[assetId as Caip19AssetId] = {
amount: balance.amount,
...(balance.metadata ? { metadata: balance.metadata } : {}),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when persist balance data via event, we persist metadata together if any

type AssetBalance already ready to store metadata


// Step 2: Get balances for those specific assets
const balances: Record<CaipAssetType, Balance> =
const balances: Record<CaipAssetType, Balance & { metadata?: Json }> =

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mock keyring type

if (accountBalances) {
(accountBalances as Record<string, unknown>)[assetId] = {
amount: balance.amount,
...(balance.metadata ? { metadata: balance.metadata } : {}),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when persist balance data via keyring API get balance, we persist metadata together if any

@Prithpal-Sooriya Prithpal-Sooriya left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice mock impl!

@stanleyyconsensys stanleyyconsensys changed the title feat: add balance metadata for snap datasource feat: add balance metadata for snap datasource [DO NOT MERGE] Jul 21, 2026
@stanleyyconsensys stanleyyconsensys self-assigned this Jul 21, 2026
@stanleyyconsensys

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a3e3158. Configure here.

accountAssets ??= {};
accountAssets[assetId as Caip19AssetId] = {
amount: balance.amount,
...(balance.metadata ? { metadata: balance.metadata } : {}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Snap metadata dropped on amount updates

Medium Severity

Balance entries written here only include metadata when the incoming snap payload provides it. Later amount-only updates still replace the whole per-asset balance object under merge, so previously stored metadata in controller state is removed even though the amount update was successful.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a3e3158. Configure here.

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.5-preview-a3e31582e
@metamask-previews/accounts-controller@39.0.5-preview-a3e31582e
@metamask-previews/address-book-controller@7.1.2-preview-a3e31582e
@metamask-previews/ai-controllers@0.8.0-preview-a3e31582e
@metamask-previews/analytics-controller@1.2.1-preview-a3e31582e
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-a3e31582e
@metamask-previews/announcement-controller@8.1.0-preview-a3e31582e
@metamask-previews/app-metadata-controller@2.0.1-preview-a3e31582e
@metamask-previews/approval-controller@9.0.2-preview-a3e31582e
@metamask-previews/assets-controller@11.2.0-preview-a3e31582e
@metamask-previews/assets-controllers@110.0.0-preview-a3e31582e
@metamask-previews/authenticated-user-storage@3.0.1-preview-a3e31582e
@metamask-previews/base-controller@9.1.0-preview-a3e31582e
@metamask-previews/base-data-service@0.1.3-preview-a3e31582e
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-a3e31582e
@metamask-previews/bridge-controller@78.0.0-preview-a3e31582e
@metamask-previews/bridge-status-controller@74.5.0-preview-a3e31582e
@metamask-previews/build-utils@3.0.4-preview-a3e31582e
@metamask-previews/chain-agnostic-permission@1.7.0-preview-a3e31582e
@metamask-previews/chomp-api-service@4.0.0-preview-a3e31582e
@metamask-previews/claims-controller@0.5.3-preview-a3e31582e
@metamask-previews/client-controller@1.0.1-preview-a3e31582e
@metamask-previews/client-utils@1.2.1-preview-a3e31582e
@metamask-previews/compliance-controller@2.1.0-preview-a3e31582e
@metamask-previews/composable-controller@12.0.1-preview-a3e31582e
@metamask-previews/config-registry-controller@0.4.1-preview-a3e31582e
@metamask-previews/connectivity-controller@0.3.0-preview-a3e31582e
@metamask-previews/controller-utils@12.3.0-preview-a3e31582e
@metamask-previews/core-backend@7.0.0-preview-a3e31582e
@metamask-previews/delegation-controller@3.0.2-preview-a3e31582e
@metamask-previews/earn-controller@12.2.2-preview-a3e31582e
@metamask-previews/eip-5792-middleware@3.0.5-preview-a3e31582e
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-a3e31582e
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-a3e31582e
@metamask-previews/ens-controller@19.1.5-preview-a3e31582e
@metamask-previews/eth-block-tracker@15.0.1-preview-a3e31582e
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-a3e31582e
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-a3e31582e
@metamask-previews/foundryup@1.0.1-preview-a3e31582e
@metamask-previews/gas-fee-controller@26.3.0-preview-a3e31582e
@metamask-previews/gator-permissions-controller@4.2.3-preview-a3e31582e
@metamask-previews/geolocation-controller@0.1.3-preview-a3e31582e
@metamask-previews/java-tron-up@1.0.0-preview-a3e31582e
@metamask-previews/json-rpc-engine@10.5.0-preview-a3e31582e
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-a3e31582e
@metamask-previews/keyring-controller@27.1.0-preview-a3e31582e
@metamask-previews/local-node-utils@1.0.0-preview-a3e31582e
@metamask-previews/logging-controller@8.0.2-preview-a3e31582e
@metamask-previews/message-manager@14.1.2-preview-a3e31582e
@metamask-previews/messenger@2.0.0-preview-a3e31582e
@metamask-previews/messenger-cli@0.2.0-preview-a3e31582e
@metamask-previews/money-account-api-data-service@0.3.0-preview-a3e31582e
@metamask-previews/money-account-balance-service@2.3.0-preview-a3e31582e
@metamask-previews/money-account-controller@0.3.3-preview-a3e31582e
@metamask-previews/money-account-upgrade-controller@3.0.0-preview-a3e31582e
@metamask-previews/multichain-account-service@13.0.0-preview-a3e31582e
@metamask-previews/multichain-api-middleware@4.0.1-preview-a3e31582e
@metamask-previews/multichain-network-controller@3.2.1-preview-a3e31582e
@metamask-previews/multichain-transactions-controller@7.1.1-preview-a3e31582e
@metamask-previews/name-controller@9.1.2-preview-a3e31582e
@metamask-previews/network-connection-banner-controller@0.1.0-preview-a3e31582e
@metamask-previews/network-controller@34.0.0-preview-a3e31582e
@metamask-previews/network-enablement-controller@5.6.0-preview-a3e31582e
@metamask-previews/notification-services-controller@26.0.0-preview-a3e31582e
@metamask-previews/passkey-controller@3.0.0-preview-a3e31582e
@metamask-previews/permission-controller@13.1.1-preview-a3e31582e
@metamask-previews/permission-log-controller@5.1.0-preview-a3e31582e
@metamask-previews/perps-controller@10.0.0-preview-a3e31582e
@metamask-previews/phishing-controller@17.3.0-preview-a3e31582e
@metamask-previews/platform-api-docs@0.0.0-preview-a3e31582e
@metamask-previews/polling-controller@16.0.8-preview-a3e31582e
@metamask-previews/preferences-controller@23.1.0-preview-a3e31582e
@metamask-previews/profile-metrics-controller@4.0.2-preview-a3e31582e
@metamask-previews/profile-sync-controller@28.3.0-preview-a3e31582e
@metamask-previews/ramps-controller@17.0.0-preview-a3e31582e
@metamask-previews/rate-limit-controller@7.0.1-preview-a3e31582e
@metamask-previews/react-data-query@0.2.2-preview-a3e31582e
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-a3e31582e
@metamask-previews/sample-controllers@5.0.3-preview-a3e31582e
@metamask-previews/seedless-onboarding-controller@10.1.0-preview-a3e31582e
@metamask-previews/selected-network-controller@26.1.5-preview-a3e31582e
@metamask-previews/sentinel-api-service@1.0.0-preview-a3e31582e
@metamask-previews/shield-controller@5.1.3-preview-a3e31582e
@metamask-previews/signature-controller@39.2.7-preview-a3e31582e
@metamask-previews/smart-transactions-controller@25.0.1-preview-a3e31582e
@metamask-previews/snap-account-service@2.0.0-preview-a3e31582e
@metamask-previews/social-controllers@2.6.0-preview-a3e31582e
@metamask-previews/solana-test-validator-up@1.0.0-preview-a3e31582e
@metamask-previews/stellar-quickstart-up@0.0.0-preview-a3e31582e
@metamask-previews/storage-service@1.0.2-preview-a3e31582e
@metamask-previews/subscription-controller@6.2.1-preview-a3e31582e
@metamask-previews/transaction-controller@69.2.1-preview-a3e31582e
@metamask-previews/transaction-pay-controller@26.0.0-preview-a3e31582e
@metamask-previews/user-operation-controller@41.2.7-preview-a3e31582e
@metamask-previews/wallet@8.1.0-preview-a3e31582e
@metamask-previews/wallet-cli@0.0.0-preview-a3e31582e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants